<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/9 0009
 * Time: 下午 3:08
 */
namespace App\Http\Admin\Customer\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Http\Admin\Website\Models\Menu;
use App\Eloquent\Ygt\Customer;
use App\Engine\Customer as CustomerEngine;
use App\Engine\Func;
use App\Eloquent\Province;
use App\Eloquent\City;
use App\Eloquent\Area;
use App\Eloquent\Ygt\User;

class IndexController extends Controller
{
    protected $modelName = 'Customer';
    protected $company = true;

    protected $rules = [
        'customer_name' => 'bail|required',
        'company_name' => 'bail|required',
        'sort_id' => 'required',
    ];

    public function __construct()
    {
        parent::__construct();
        $this->model = new Customer();

    }

    protected function _where(){

        $where = [];
        if($customerName = request('customer_name')){
            $where['customer_name'] = ['like','%'.$customerName.'%'];
        }
        if($salesUid = request('sales_uid')){
            $where['sales_uid'] = $salesUid;
        }
        return $where;
    }

    protected function _hdata($result){
        $result->transform(function ($item, $key) {
            $item->avatars_url = Func::getImgUrlById($item->avatars_id);
            $item->customer_level_title = CustomerEngine::getLevelTitle($item->customer_level);
            $item->sales_name = isset($item->sales->truename)?$item->sales->truename:'';
            $item->merchandiser_name = isset($item->merchandiser->truename)?$item->merchandiser->truename:'';
            $item->last_order_time = $item->last_order_time?date('Y-m-d',$item->last_order_time):'';
            return $item;
        });
        return $result;
    }

    protected function _export_title(){
        return [
            'customer_name'=>'客户名称',
            'company_name'=>'单位名称',
            'customer_no'=>'客户编号',
            'customer_tel'=>'客户电话',
            'customer_address'=>'客户地址',
            'contact_person'=>'客户代表',
            'contact_phone'=>'客户代表电话',
            'contact_address'=>'客户代表地址',
            'sales_name'=>'销售员',
            'sort_id'=>'排序',
            'last_order_time'=>'最近下单时间',
        ];
    }

    protected function _honedata($info){
        $info->sales_name = isset($info->sales->truename)?$info->sales->truename:'';
        $info->avatars_url = Func::getImgUrlById($info->avatars_id);
        return $info;
    }

    protected function _vdataAdd(){
        $levels = CustomerEngine::getAllLevel();
        $provinceList           = Province::all();
        return ['levels'=>$levels,'provinceList'=>$provinceList];
    }

    protected function _cdata(){
        $provinceInfo = Province::where('provinceID',request('province_id'))->first();
        $cityInfo = City::where('cityID',request('city_id'))->first();
        $areaInfo = Area::where('areaID',request('area_id'))->first();
        $pinyin = app('pinyin');
        $firstLetter = strtoupper(substr($pinyin->abbr(request('customer_name')),0,1));
        $merchandiserUid = request('merchandiser_uid');
        return [
            'province_name' => isset($provinceInfo->province)?$provinceInfo->province:'',
            'city_name' => isset($cityInfo->city)?$cityInfo->city:'',
            'area_name' => isset($areaInfo->area)?$areaInfo->area:'',
            'first_letter'=>$firstLetter,
            'merchandiser_uid'=>$merchandiserUid?$merchandiserUid:0,
        ];
    }

    public function setPassword(){
        $id = request('id');
        $password               = request('password');

        $where                  = ['id'=>$id];
        $info                   = User::getInfo($where);
        $result             = ['message'=>'编辑失败'];
        if($info){
            //更新密码
            $salt               = mt_rand( 1000, 9999 );
            $password_md5       = User::passwordMd5($password,$salt);
            $user_data          = [ 'password'=>$password_md5, 'salt'=>$salt ];
            $update_result      = User::updateOneData( $where, $user_data );
            if( $update_result ){
                $result         = ['message'=>'密码修改成功'];
            }
        }

        return $result;
    }
}